//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2013 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_AUTO_IO_H_
#define _BUOLA_AUTO_IO_H_

#include <buola/auto/forward.h>

#include <string>

namespace buola { 
    
///\addtogroup io
///@{

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///
/// I/O related functions and classes
///
/// This contains all functions and classes related to input and output
///
/// It also contains enumerations and symbols to be used as parameters to those functions
///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace io {

static const ssize_t cEOF=-1;
static const ssize_t cWouldBlock=-2;

///specifies the origin for stream displacements
enum class ESeek
{
    ABSOLUTE,   ///<from beginning of data
    RELATIVE,   ///<from current position
    END         ///<from end of file
};

enum class ESocketType
{
    NONE=0,
    PACKET=(1<<0)
};

DECLARE_ENUM_CLASS_BITOPS(ESocketType);

///flags used to request notification on IO
enum class ENotify
{
    NONE=0,
    READ=(1<<0),
    WRITE=(1<<1),
    EXCEPTION=(1<<2)
};

DECLARE_ENUM_CLASS_BITOPS(ENotify);

///controls how files are open
enum class EMode
{
    NONE=0,
    OPEN=(1<<0),                    ///<open an existing file
    CREATE=(1<<1),                  ///<create file
    OPENORCREATE=OPEN|CREATE,       ///<open existing file, or create if it doesn't exist
    READ=(1<<2),                    ///<reading is possible
    WRITE=(1<<3),                   ///<writing is possible
    EXEC=(1<<4),                    ///<allow execution (valid for shared memory access)
    READWRITE=READ|WRITE,           ///<reading and writing possible
    APPEND=(1<<5)|WRITE,            ///<only append to end of file (implies MODE_WRITE)
    TRUNCATE=(1<<6)|WRITE,          ///<clear the file if it already exists (implies MODE_WRITE)
    NONBLOCK=(1<<7),                ///<never block until data becomes available
    SYNC=(1<<10),                   ///<immediately flush to media
    PRIVATE=(1<<11),                ///<map memory in private mode
    FIXEDADDRESS=(1<<12),           ///<map memory at a fixed address
    KEEPONEXEC=(1<<13)              ///<don't close on exec()
};

DECLARE_ENUM_CLASS_BITOPS(EMode);

///some extra flags for read operations
enum class EReadFlags
{
    NONE=0,
    EXTRACTLAST=(1<<0),             ///<extract the character that matches
    INCLUDELAST=(1<<1),             ///<include the character that matches in the returned sequence (implies EXTRACTLAST)
};

DECLARE_ENUM_CLASS_BITOPS(EReadFlags);

///permission mask
enum class EPermission
{
    NONE=0,
    UA=0700,
    UR=0400,
    UW=0200,
    URW=0600,
    UX=0100,
    GA=0070,
    GR=0040,
    GW=0020,
    GRW=0060,
    GX=0010,
    OA=0007,
    OR=0004,
    OW=0002,
    ORW=0006,
    OX=0001,
    ALL=0777,
    R=0444,
    W=0222,
    X=0111,
    RW=0666
};

DECLARE_ENUM_CLASS_BITOPS(EPermission);

///specifies the operation to combine the new notify mask with the current one
enum class ENotifyOp
{
    SET,
    OR,
    AND,
    CLEAR
};

enum class EFileType
{
    UNKNOWN=-1,
    NONE=0,
    REGULAR=(1<<0),
    LINK=(1<<1),
    FOLDER=(1<<2),
    BLOCK=(1<<3),
    CHAR=(1<<4),
    SOCKET=(1<<5),
    FIFO=(1<<6)
};

DECLARE_ENUM_CLASS_BITOPS(EFileType);

/*namespace io*/ } 

extern io::CTextReader gIn;
extern io::CTextWriter gOut;
extern io::CTextWriter gErr;

///@}

/*namespace buola*/ }

#endif
